

function add_tdiv(x, y, w, h, title, hide, tag, css, callback, divid) {
    // 确保参数有默认值
    x = x || 100;
    y = y || 100;
    w = w || 200;
    h = h || 150;
    title = title || '卡片面板';
    hide = hide || false;
    tag = tag || '';
    css = css || '';
    callback = callback || null;
    
    // 生成唯一ID
    const id = 'tdiv_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
    
    // 创建卡片元素
    const tdiv = document.createElement('div');
    tdiv.id = id;
    tdiv.className = 'layui-card';
    
    // 创建卡片头部
    const header = document.createElement('div');
    header.className = 'layui-card-header';
    header.textContent = title;
    
    // 创建卡片主体
    const body = document.createElement('div');
    body.className = 'layui-card-body';
    
    // 组装卡片结构
    tdiv.appendChild(header);
    tdiv.appendChild(body);
    
    // 设置样式
    tdiv.style.position = 'absolute';
    tdiv.style.left = x + 'px';
    tdiv.style.top = y + 'px';
    tdiv.style.width = w + 'px';
    tdiv.style.height = h + 'px';
    
    // 附加样式和标记
    if (css) tdiv.style.cssText += css;
    if (tag) tdiv.setAttribute('data-tag', tag);
    if (hide) tdiv.style.display = 'none';
    
    // 事件处理
    tdiv.onmousemove = function(e) { handleEvent(id, 0, title, tag, e.clientX, e.clientY, null, callback, divid); };
    tdiv.onclick = function(e) { handleEvent(id, 1, title, tag, e.clientX, e.clientY, null, callback, divid); };
    tdiv.oncontextmenu = function(e) { e.preventDefault(); handleEvent(id, 2, title, tag, e.clientX, e.clientY, null, callback, divid); return false; };
    tdiv.onmouseover = function(e) { handleEvent(id, 5, title, tag, e.clientX, e.clientY, null, callback, divid); };
    tdiv.onmouseout = function(e) { handleEvent(id, 6, title, tag, e.clientX, e.clientY, null, callback, divid); };
    tdiv.onkeydown = function(e) { handleEvent(id, 3, title, tag, 0, 0, e.key, callback, divid); };
    tdiv.onkeyup = function(e) { handleEvent(id, 4, title, tag, 0, 0, e.key, callback, divid); };
    
    // 添加到页面或指定父div
    const parent = divid ? document.getElementById(divid) : document.body;
    parent.appendChild(tdiv);
    
    // 初始化tdivs存储空间
    if (!elementStore.tdivs) {
        elementStore.tdivs = {};
    }
    
    // 存储引用
    elementStore.tdivs[id] = tdiv;
    
    return id;
}

window.del_tdiv = function(id) {
    if (elementStore.tdivs[id]) {
        elementStore.tdivs[id].remove();
        delete elementStore.tdivs[id];
    }
}

window.upd_tdiv = function(id, x, y, w, h, title, hide, tag, css) {
    const tdiv = elementStore.tdivs[id];
    if (tdiv) {
        if (x !== undefined) tdiv.style.left = x + 'px';
        if (y !== undefined) tdiv.style.top = y + 'px';
        if (w !== undefined) tdiv.style.width = w + 'px';
        if (h !== undefined) tdiv.style.height = h + 'px';
        if (title !== undefined) tdiv.querySelector('.layui-card-header').textContent = title;
        if (hide !== undefined) tdiv.style.display = hide ? 'none' : 'block';
        if (tag !== undefined) tdiv.setAttribute('data-tag', tag);
        if (css !== undefined) tdiv.style.cssText = css;
    }
}